package android.tusnotas.activities;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.tusnotas.activities.extras.ShowInstancesActivityExtras;
import android.tusnotas.beans.Instance;
import android.tusnotas.core.GradeAnalyser;
import android.tusnotas.services.WebCheckerManager;
import android.tusnotas.util.NetworkManager;
import android.tusnotas.util.PropertyLoader;
import android.util.Log;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class Welcome extends Activity {

	private static boolean ACTIVE = true;
	private static final Integer DURATION = 4000;

	private ImageView imageViewLogo = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

		WebCheckerManager webChecker = new WebCheckerManager(this);
		if (webChecker.isWebCheckerStarted()){
			Log.e("","web checker is started *************************");

			try {
				Intent showInstancesIntent =  new Intent(getApplicationContext(), ShowInstancesActivity.class);
				startActivity(showInstancesIntent);
			} catch (Exception e) {
				e.printStackTrace();
			}
			finish();
		}
		else{
			Log.i("","web checker is not started *************************");

			//TODO etapa 3: modify the layout background
			setContentView(R.layout.welcome_splash);
			imageViewLogo = (ImageView) findViewById(R.id.welcome_logo);

			Animation logoAnimation = AnimationUtils.loadAnimation(this, R.anim.welcome_logo_anim);
			imageViewLogo.startAnimation(logoAnimation);

			Thread splashThread = new Thread() {
				@Override
				public void run() {
					try {
						Integer waited = 0;
						while (ACTIVE && (waited < DURATION)) {
							sleep(500);
							waited += 500;
						}
					} catch (InterruptedException e) {
						// do nothing
					} finally {
//						ACTIVE = true;
//						finish();
						// get hash
						PropertyLoader.getInstance(getApplicationContext());
						String hash = PropertyLoader.HASH;
						Log.e("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.* Saved HASH: ",hash);
						// there is no hash: show login screen
						if (hash.equals("")){
							ACTIVE = true;
							finish();
							Log.e("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*","hash is empty");
							Intent i;
							try {
								i = new Intent(Welcome.this, Login.class);
								startActivity(i);
							} catch (Exception e) {
								e.printStackTrace();
							}
							// if there is hash:
						} else {
							try {
								Log.e("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*","hash is NOT empty");
								GradeAnalyser gradeAnalyser = new GradeAnalyser();
								List<Instance> instances = gradeAnalyser.doGetGrades();
								if(instances !=null ){
									gradeAnalyser.doAnaliseGrades(instances, getApplicationContext());
								}
							} catch (Exception e){
								e.printStackTrace();
								Log.e("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*","saved hash login exception");
                                ACTIVE = true;
                                finish();
                                Intent i;
                                try {
                                    i = new Intent(Welcome.this, Login.class);
                                    startActivity(i);
                                } catch (Exception exc) {
                                    exc.printStackTrace();
                                }
                                return;
                            }
							ACTIVE = true;
							finish();
							// push subscription here
							Intent showInstancesIntent =  new Intent(Welcome.this, ShowInstancesActivity.class);
							showInstancesIntent.putExtra(ShowInstancesActivityExtras.FROM_LOGIN, Boolean.TRUE);
							Log.e("*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*","Starting instances activity from saved hash");
							startActivity(showInstancesIntent);
						}
					}
				}
			};
			splashThread.start();
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			ACTIVE = false;
		}

		return true;
	}

	@Override
	public void onPause()
	{
		super.onPause();
		Log.i(" **************************************** Pausing welcome", "");
	}

	@Override
	public void onResume()
	{
		Log.i(" **************************************** Resuming welcome", "");
		super.onResume();
	}

}
